import os
import subprocess
from PIL import Image, ImageTk


class AppCon:
    #FileList, Honeyviewpath
    
    def __init__(self, bh):        
        print("AppCon test setting!")
        self.beehive = bh
        self.FileList = ["C:/aaa/aaa.aaa", "C:/bbb/bbb.bbb"]
        self.Honeyviewpath = ""
        self.os = os.name
        self.abstract_list = []
        self.detail_list = []
        #self.LabelImage        
                
    def getFiles(self, files):                
        tempstringlist = str(files).split(" ")
        trimlist = []
        
        i=0
        while i<len(tempstringlist):
            if tempstringlist[i][0] == "{":
                tempst=tempstringlist[i]
                for j in range(i+1, len(tempstringlist)):
                    tempst = tempst + " " + tempstringlist[j]
                    if tempstringlist[j][len(tempstringlist[j])-1] == "}":
                        i = j + 1
                        break
                trimlist.append(tempst[1:len(tempst)-1])
            else:
                trimlist.append(tempstringlist[i])
                i = i+1
                
        print(trimlist)
        self.FileList.extend(trimlist)
    
    def getFolder(self, dirpath):
        self.beehive.archive_append(dirpath)                      
        
        
    def sethoneyviewpath(self, *args):
        honeyviewpathlist = ["C:/Program Files/Honeyview/Honeyview.exe.test", "C:/Program Files/Honeyview/Honeyview.exe"]
        if len(args) == 0:
            for path in honeyviewpathlist:
                if os.path.isfile(path):
                    self.Honeyviewpath = path
                    print(self.Honeyviewpath)
                    return "setting"
            return "cannot_find_auto"
        elif len(args) == 1:
            self.Honeyviewpath = str(args[0])
            print(self.Honeyviewpath)
            return "setting"
        else:
            return "error"
        
    def refresh(self): #BH is Beehive object
        self.beehive.rebuilding()
        
        for item in self.beehive.Comics:
            self.abstract_list.append(item.GetInfoByDic())
            
        return self.abstract_list        
        
    def AbstractTreeViewClick(self, clicked_iid):
        print('clicked')
        self.detail_list.clear()
        if clicked_iid == ('All'):
            for Comic in self.abstract_list:
                for Book in Comic['Books']:
                    self.detail_list.append(Book.GetInfoByDic())
            
        else:
            for s_Comic in self.abstract_list:
                if s_Comic['iid'] == clicked_iid:
                    for s_Book in s_Comic['Books']:
                        self.detail_list.append(s_Book.GetInfoByDic())
                    break
        pass
    
    def DetailTreeViewClick(self, clicked_iid):
        for s_Book in self.detail_list:
            if s_Book['iid'] == clicked_iid:
                self.LabelImage = s_Book['Image']
        pass
                    
    def DetailTreeViewDoubleClick(self, clicked_iid):
        if self.Honeyviewpath == '':
            print('error: set Honeyview path')
            return 'error: set Honeyview path'
        for item in self.detail_list:
            if clicked_iid == item['iid']:
                subprocess.Popen([self.Honeyviewpath, self.ModifyPath(item['Path'])])
                #subprocess.Popen(['C:/Program Files/Honeyview/Honeyview.exe','D:/workspace/comic-beehive/comic-beehive-python/dummydata/aaa/1'])
                break
        pass
    
    def setNoObjectImage(self):
        self.LabelImage = self.getImageInfo('./app/no_object.jpg')
        pass
    
    def getImageInfo(self, path, size = (200,200)):
        try:
            image_info = Image.open(path)
            image_info.thumbnail(size, Image.ANTIALIAS)
            photo = ImageTk.PhotoImage(image_info)
            return photo
        except FileNotFoundError:
            print("Can't find image")
            return
    
    def ModifyPath(self, path):
        os.name
        if os.name == 'nt':
            return path.replace("\\", "/")
        pass